package com.bite.user.listenner;

import com.bite.common.utils.JsonUtil;
import com.bite.common.utils.Mail;
import com.bite.user.constants.Constants;
import com.bite.user.dataobject.UserInfo;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class UserQueueListener {

    @Autowired
    private Mail mail;

    @RabbitListener(queues = Constants.USER_QUEUE_NAME)
    public void handlerEmailSend(Message message, Channel channel) {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            String body = new String(message.getBody());
            log.info("接收到消息: {}", body);
            //发送邮件
            UserInfo userInfo = JsonUtil.parseJson(body, UserInfo.class);
            mail.send(userInfo.getEmail(), "恭喜加入比特博客社区", buildContent(userInfo.getUserName()));
            channel.basicAck(deliveryTag, true);
        } catch (Exception e) {
            try {
                log.error("消息处理失败，邮件发送失败, e:", e);
                channel.basicNack(deliveryTag, true, true);
            } catch (IOException ex) {
                log.error("拒绝消息失败, e:", ex);
            }
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = Constants.USER_QUEUE_NAME, durable = "true"),
            exchange = @Exchange(value = Constants.USER_EXCHANGE_NAME, type = ExchangeTypes.FANOUT)
    ))
    public void handlerEmailSendVto(Message message, Channel channel) {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            String body = new String(message.getBody());
            log.info("接收到消息: {}", body);
            //发送邮件
            UserInfo userInfo = JsonUtil.parseJson(body, UserInfo.class);
            mail.send(userInfo.getEmail(), "恭喜加入比特博客社区", buildContent(userInfo.getUserName()));
            channel.basicAck(deliveryTag, true);
        } catch (Exception e) {
            try {
                log.error("消息处理失败，邮件发送失败, e:", e);
                channel.basicNack(deliveryTag, true, true);
            } catch (IOException ex) {
                log.error("拒绝消息失败, e:", ex);
            }
        }
    }

    private String buildContent(String userName) {
        StringBuilder content = new StringBuilder();
        content.append("尊敬的").append(userName).append(", 您好！").append("<br/>");
        content.append("感谢您注册成为我们博客社区的一员！我们很高兴您加入我们的大家庭！<br/>");
        content.append("您的注册信息如下： 用户名：").append(userName).append("<br/>");
        content.append("为了您的账号安全，请妥善保管您的登录信息！");
        return content.toString();
    }
}
